package com.heima.article.config;

import lombok.extern.log4j.Log4j2;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.Map;

/**
 * 动态数据源
 * 
 * @author ruoyi
 */
@Log4j2
public class DynamicDataSource extends AbstractRoutingDataSource
{
    public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources)
    {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
    }
    @Override
    protected Object determineCurrentLookupKey()
    {
        // 从Thread Local变量中  取出AOP拦截时 所存储的数据源名称  master    slave
        String key = DynamicDataSourceContextHolder.getDataSourceType();
        log.info("确认当前线程使用数据源==>{}",key);
        return key;
    }
}